package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.Aboutus;
import com.adingxiong.quickstart.project.service.IAboutusService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 关于我们 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-06-01
 */
@CrossOrigin
@RestController
@RequestMapping("/aboutus")
@Api(tags = "关于我们")
public class AboutusController {

    @Autowired
    private IAboutusService service;



    @PostMapping("/save")
    @ApiOperation(value = "保存关于我们的信息")
    public Result saveInfo(@RequestBody Aboutus aboutus){
        if(aboutus == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        aboutus.setIsValid(2);
        boolean insert = service.save(aboutus);
        return new Result(insert ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),insert ? "新增成功" : "新增失败",insert);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改关于我们的信息")
    public Result updateInfo(@RequestBody Aboutus aboutus){
        if(aboutus == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean update = service.updateById(aboutus);
        return new Result(update ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),update ? "修改成功" : "修改失败",update);
    }

    @GetMapping("/del")
    @ApiOperation(value = "删除多条")
    public Result dels(
            @ApiParam(value = "id数组" ,required = true)  @RequestParam(value = "ids") String ids){
        if(StringUtils.isEmpty(ids)){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        List arr = Arrays.asList(ids.split(","));
        boolean del = service.removeByIds(arr);
        return new Result(del ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),del ? "删除成功" : "删除失败",del);
    }

    @ApiOperation(value = "高级查询")
    @GetMapping("/list")
    public Result<Page<Aboutus>> getList(
            @ApiParam(value = "关键字") @RequestParam(value = "keywords" ,required = false) String keywords,
            @ApiParam(value = "当前页码") @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            @ApiParam(value = "每页数量") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ){
        QueryWrapper<Aboutus> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keywords)) {
            queryWrapper.like("com_name",keywords).or().like("address",keywords);
        }
        Page page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        Page<Aboutus> res = service.page(page,queryWrapper);
        return new Result<>(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }
}

